package me.numark.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.R;

/**
 * Created by tian on 14-9-18:上午7:43.
 */
public class DiscoverFragment extends Fragment implements View.OnClickListener, RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {


    @InjectView(R.id.pager)
    ViewPager pager;

    @InjectView(R.id.radio_left)
    RadioButton leftRadio;

    @InjectView(R.id.radio_right)
    RadioButton rightRadio;

    @InjectView(R.id.radio_group)
    RadioGroup radioGroup;

    DiscoverFragmentPageAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_discover, container, false);
        ButterKnife.inject(this, v);
        mAdapter = new DiscoverFragmentPageAdapter();
        pager.setAdapter(mAdapter);
        pager.addOnPageChangeListener(this);
        leftRadio.setText(mAdapter.getPageTitle(0));
        rightRadio.setText(mAdapter.getPageTitle(1));
        radioGroup.setOnCheckedChangeListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {

    }

    private NewsFeedFragment mFeedFragment;

    private NewsFeedFragment mHotFragment;

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.radio_right) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        if (i == 1) {
            radioGroup.check(R.id.radio_right);
        } else {
            radioGroup.check(R.id.radio_left);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    private class DiscoverFragmentPageAdapter extends FragmentPagerAdapter {

        public DiscoverFragmentPageAdapter() {
            super(getChildFragmentManager());
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return getFeedFragment();
                case 1:
                    return getHotFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.new_timeline);
                case 1:
                    return getString(R.string.hot_timeline);
                default:
                    return "";
            }
        }
    }

    private Fragment getFeedFragment() {
        Bundle args = new Bundle();
        args.putInt(NewsFeedFragment.TYPE, NewsFeedFragment.TYPE_TIMELINE);
        if (mFeedFragment == null) {
            mFeedFragment = (NewsFeedFragment) NewsFeedFragment
                    .instantiate(getActivity(), NewsFeedFragment.class.getName(), args);
        }
        return mFeedFragment;
    }

    private Fragment getHotFragment() {
        Bundle args = new Bundle();
        args.putInt(NewsFeedFragment.TYPE, NewsFeedFragment.TYPE_HOT);
        if (mHotFragment == null) {
            mHotFragment = (NewsFeedFragment) NewsFeedFragment
                    .instantiate(getActivity(), NewsFeedFragment.class.getName(), args);
        }
        return mHotFragment;
    }

}
